package edu.lnu.project.timeeditapp.EventDialog;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.SparseIntArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.lnu.project.timeeditapp.R;

import edu.lnu.project.timeeditapp.activites.ReminderService;
import edu.lnu.project.timeeditapp.db.TimeEditDataSource;
import edu.lnu.project.timeeditapp.helper.MyHelper;
import edu.lnu.project.timeeditapp.parser.Event;



public class EventDetailedDialog extends Dialog {

	private Activity activity;
	private Event mEvent;
	Builder build;
	public ImageButton reminderIcon;
	public SparseIntArray reminders;
	int reminder = 15;
	TimeEditDataSource datasource;
	boolean eventExpired;
	boolean reminderRadioVisible;

	public EventDetailedDialog(Activity a, Event event) {
		super(a);

		this.activity = a;
		this.mEvent = event;
		reminders = new SparseIntArray();
		reminders.put(0, 5);
		reminders.put(1, 15);
		reminders.put(2, 30);
		reminders.put(3, 60);
		datasource = new TimeEditDataSource(activity);
		eventExpired = false;
		reminderRadioVisible = true;

		Window window = this.getWindow();
		window.setFlags(WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
				WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

	}

	@SuppressWarnings("deprecation")
	@Override
	public void show() {

		datasource.open();
		mEvent = datasource.getEvent(mEvent.getId());
		datasource.close();

		eventExpired = MyHelper.checkEventExpired(mEvent.getBeginDate());

		final Resources res = activity.getResources();

		LayoutInflater inflater = LayoutInflater.from(activity);
		final View dialogView = inflater.inflate(R.layout.event_dialog, null);
		TextView time = (TextView)dialogView.findViewById(R.id.eventDetailedViewTime);
		TextView teacher = (TextView)dialogView.findViewById(R.id.eventDetailedViewTeacher);
		TextView city = (TextView)dialogView.findViewById(R.id.eventDetailedViewCity);
		TextView comment = (TextView)dialogView.findViewById(R.id.eventDetailedViewComment);
		TextView room = (TextView)dialogView.findViewById(R.id.eventDetailedViewRoom);
		TextView name = (TextView)dialogView.findViewById(R.id.eventDetailedViewName);

		time.setText("Begin time - End time: "+mEvent.getBeginTime().toString()+" - "+mEvent.getEndTime().toString());
		teacher.setText("Teacher: "+mEvent.getTeacher().toString());
		city.setText("City: "+mEvent.getCity().toString());
		comment.setText("Comments: "+mEvent.getComments().toString());
		room.setText("Room: "+mEvent.getRoom().toString());
		name.setText(mEvent.getCourse());

		reminder = mEvent.getReminder();
		final RadioGroup reminderRadioGroup = (RadioGroup) dialogView.findViewById(R.id.reminder_radio_group);
		RadioButton fiveReminderButton = (RadioButton) dialogView.findViewById(R.id.fiveRadio);//Maybe delete this option
		RadioButton fifteenReminderButton = (RadioButton) dialogView.findViewById(R.id.fifteenRadio);
		RadioButton thirtyReminderButton = (RadioButton) dialogView.findViewById(R.id.thirtyRadio);
		RadioButton hourReminderButton = (RadioButton) dialogView.findViewById(R.id.hourRadio);

		//disable unavailable reminder options
		if(!MyHelper.isReminderAvailable(60, mEvent)){
			hourReminderButton.setVisibility(View.GONE);
		}
		if(!MyHelper.isReminderAvailable(30, mEvent)){
			thirtyReminderButton.setVisibility(View.GONE);

		}
		if(!MyHelper.isReminderAvailable(15, mEvent)){
			fifteenReminderButton.setVisibility(View.GONE);

		}
		if(!MyHelper.isReminderAvailable(5, mEvent)){
			fiveReminderButton.setVisibility(View.GONE);
			reminderRadioGroup.setVisibility(View.GONE);
			reminderRadioVisible = false;
		}

		if(!eventExpired){
			switch (reminder) {
			case 60: 
				hourReminderButton.setChecked(true);
				thirtyReminderButton.setChecked(false);
				fifteenReminderButton.setChecked(false);
				fiveReminderButton.setChecked(false);
				break;
			case 30: 
				hourReminderButton.setChecked(false);
				thirtyReminderButton.setChecked(true);
				fifteenReminderButton.setChecked(false);
				fiveReminderButton.setChecked(false);
				break;
			case 15: 
				hourReminderButton.setChecked(false);
				thirtyReminderButton.setChecked(false);
				fifteenReminderButton.setChecked(true);
				fiveReminderButton.setChecked(false);
				break;
			case 5: 
				hourReminderButton.setChecked(false);
				thirtyReminderButton.setChecked(false);
				fifteenReminderButton.setChecked(false);
				fiveReminderButton.setChecked(true);
				break;
			default:
				hourReminderButton.setChecked(false);
				thirtyReminderButton.setChecked(false);
				fifteenReminderButton.setChecked(true);
				fiveReminderButton.setChecked(false);
				break;
			}
		}
		else{
			reminderRadioGroup.setVisibility(View.GONE);
			reminderRadioVisible = false;
		}

		fiveReminderButton.setOnClickListener(new reminderRadioClickListener());
		fifteenReminderButton.setOnClickListener(new reminderRadioClickListener());
		thirtyReminderButton.setOnClickListener(new reminderRadioClickListener());
		hourReminderButton.setOnClickListener(new reminderRadioClickListener());


		Button remindMe = (Button)dialogView.findViewById(R.id.remindMe);
		Button dontRemind = (Button)dialogView.findViewById(R.id.dontRemind);
		final TextView reminderIcon = (TextView)dialogView.findViewById(R.id.reminderIcon);


		if(!eventExpired && reminderRadioVisible ){

			if(reminder>0){
				System.out.println();
				reminderIcon.setBackgroundDrawable(res.getDrawable(R.drawable.alarm_on));
			}
			else{
				reminderIcon.setBackgroundDrawable(res.getDrawable(R.drawable.alarm_off));
			}

			remindMe.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					if(reminder == 0){
						reminder = 15;
					}
					reminderIcon.setBackgroundDrawable(res.getDrawable(R.drawable.alarm_on));
					//Reminder is set
					updateReminder(mEvent, reminder);

				}
			});

			dontRemind.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					reminderIcon.setBackgroundDrawable(res.getDrawable(R.drawable.alarm_off));
					//Reminder is cleared
					updateReminder(mEvent, 0);
				}
			});
		}
		else{
			remindMe.setVisibility(View.GONE);
			dontRemind.setVisibility(View.GONE);
		}

		build = new Builder(activity);
		build.setView(dialogView);
		build.setNeutralButton(android.R.string.ok, null);
		build.show();
	}

	public void updateReminder(Event event, int reminder){
		if(reminder == 0 || eventExpired){
			datasource.open();
			datasource.updateEventReminder(event, 0);
			datasource.close();

			//for changes that affects the reminder service
			//immediately istead of retrieving from database
			ReminderService.allEvents.get(event.getId()).setReminder(0); 
		}
		else{
			datasource.open();
			datasource.updateEventReminder(event, reminder);
			datasource.close();

			//for changes that affects the reminder service
			//immediately istead of retrieving from database
			ReminderService.allEvents.get(event.getId()).setReminder(reminder); 
		}

	}

	class reminderRadioClickListener implements View.OnClickListener{

		@Override
		public void onClick(View v) {

			if(((RadioButton)v).isChecked()){
				reminder = reminders.get(Integer.parseInt(v.getTag().toString()));

			}
		}
	}
}